# _*_ coding: utf-8 _*_
#!/usr/bin/env python

import wx
import sys, os
import pickle

TILESIZE			= 8, 8
TILESET_PATH		= sys.argv[1] #'../Tileset_Donut_Plains.png'
MAP_PATH			= sys.argv[2] #'Donut_Plains_3.png'

# Braucht man nur, wenn das Tileset automatisch erzeut werden soll
NEW_TILEMAP_SIZE	= 32, 32


# Aus Portabilitaetsgruenden. Haengt das Arbeitsverzeichnis vor den Dateinamen
def addWorkPath(filename):
	return os.path.join(os.path.dirname(sys.argv[0]), filename)

map_img						= wx.Image(addWorkPath(MAP_PATH)) #, wx.PNGHandler)
map_width, map_height		= map_img.GetWidth() / TILESIZE[0], map_img.GetHeight() / TILESIZE[1]
mapdata						= []
tile_array					= []

if True: #os.access(addWorkPath(TILESET_PATH), os.F_OK):
	tileset_img				= wx.Image(addWorkPath(TILESET_PATH))
	for y in range(tileset_img.GetHeight() / TILESIZE[1]):
		for x in range(tileset_img.GetWidth() / TILESIZE[0]):
			tile_array.append(tileset_img.GetSubImage((TILESIZE[0] * x, TILESIZE[1] * y, TILESIZE[0], TILESIZE[1])).GetData())
#else:
	## Ermal ein neues Tileset erzeugen
	#tileset_img				= wx.EmptyImage(NEW_TILEMAP_SIZE[0] * TILESIZE[0], NEW_TILEMAP_SIZE[1] * TILESIZE[1])
	#stringdata				= '' #((NEW_TILEMAP_SIZE[0] * NEW_TILEMAP_SIZE[1]) * 3) * ord(0x00)
	#for y in range(map_height):
		#for x in range(map_width):
			#tile			= map_img.GetSubImage((TILESIZE[0] * x, TILESIZE[1] * y, TILESIZE[0], TILESIZE[1])).GetData()
			#if not tile in tile_array:
				#tile_array.append(tile)
				#stringdata += tile
	##print len(tileset_img.GetData()), len(stringdata), len(stringdata + tileset_img.GetData()[len(stringdata):])
	#targetstring			= ''
	#for y in range(NEW_TILEMAP_SIZE[1] * TILESIZE[1]):
		#for x in range(NEW_TILEMAP_SIZE[0] * TILESIZE[0]):
			#targetstring += stringdata[TILESIZE[1] * y + x]
	
	#tileset_img.SetData(stringdata + tileset_img.GetData()[len(stringdata):])
	#tileset_img.SaveFile(addWorkPath(TILESET_PATH), wx.BITMAP_TYPE_PNG)

#for y in range(map_height):
	#mapdata.append([])
	#for x in range(map_width):
		#try:
			#mapdata[-1].append(tile_array.index(map_img.GetSubImage((TILESIZE[0] * x, TILESIZE[1] * y, TILESIZE[0], TILESIZE[1])).GetData()))
		#except:
			#mapdata[-1].append(0)


file		= file(addWorkPath('../' + MAP_PATH.split('.')[0] + '.map.xml'), 'w')
file.write(
'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Dies ist eine Mapdatei für unser Retro Racer Projekt [Hendrik Leibrandt] -->

<map>
	<titel>Mario Circuit 1</titel>
	<rounds>3</rounds>
	<startpositions>
		<position id="1" xpos="15" ypos="50" />
	</startpositions>
	<checkpoints>
		<point id="0" xpos1="10" ypos1="10" xpos2="50" ypos2="50" />
	</checkpoints>
	
	<!-- Ressourcenangaben -->
	<tileset	scr="./Tilemap_Mario_Circuit.png" />
	<horizon	scr="" />
	<musicfile	scr="" />
	
	<!-- Grafikangaben -->
	<fog distance="0" color="0xFFFFFF" />
	
	<mapdata xsize="32" ysize="32" edgetile="0x00">
''')
for y in range(map_height):
	file.write('		<tilerow>\n')
	for x in range(map_width):
		try:
			tile = tile_array.index(map_img.GetSubImage((TILESIZE[0] * x, TILESIZE[1] * y, TILESIZE[0], TILESIZE[1])).GetData())
		except:
			tile = 0
		#file.write('			<tile>0x%.4X</tile>\n' % tile)
		file.write('			<tile>%i</tile>\n' % tile)
	file.write('		</tilerow>\n')

file.write(
'''	</mapdata>
</map>
''')
file.close()
